package com.bit.bit_forum.controller;

import com.bit.bit_forum.common.AppResult;
import com.bit.bit_forum.common.ResultCode;
import com.bit.bit_forum.config.AppConfig;
import com.bit.bit_forum.model.Article;
import com.bit.bit_forum.model.ArticleReply;
import com.bit.bit_forum.model.User;
import com.bit.bit_forum.service.IArticleReplyService;
import com.bit.bit_forum.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api
@RestController
@RequestMapping("/articleReply")
public class ArticleReplyController {

    @Autowired
    private IArticleReplyService articleReplyService;
    @Autowired
    private IArticleService articleService;
    @RequestMapping("/create")
    public AppResult create(HttpServletRequest req,
                            @ApiParam("文章id")  @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复内容") @RequestParam("content") @NonNull String content){
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //对帖子校验:
        Article article = articleService.selectById(articleId);
        if(article==null || article.getDeleteStat()==1){
            //贴子不存在:
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState()==1){
            //帖子状况异常:
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setPostUserId(user.getId());
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    @RequestMapping("/selectAllByArticleId")
    public AppResult<List<ArticleReply>> selectAllByArticleId(@RequestParam("articleId") @NonNull Long articleId){
        Article article = articleService.selectById(articleId);
        if(article==null || article.getDeleteStat()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
//        if(article.getState()==1){
//            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
//        }
        List<ArticleReply> articleReplies = articleReplyService.selectAllByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}
